/*
 *   Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *   Licensed under the GNU General Public License v3 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *        http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   under the License.
 */
package xperiment.metaphor.model.entity;

import java.io.Serializable;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.persistence.Column;
import javax.persistence.Id;
import metaphor.service.ApplicationManager;
import metaphor.service.ServiceException;
import metaphor.persistence.Dao;
import org.hibernate.LazyInitializationException;
import xperiment.metaphor.model.AbstractBean;
import xperiment.metaphor.model.ModelException;
import xperiment.metaphor.model.SaveException;
import xperiment.metaphor.model.contact.Address;
import xperiment.metaphor.model.contact.ContactInformation;

/**
 * A entity models a legal entity such as a person or a group of people such
 * as organizations or companies. Legal entities could also be clubs and
 * groups.
 *
 * @author Mark P Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class Entity extends AbstractBean {

    /* The unique database id for the object */
    @Id
    @Column(name = "entity_id", unique = true, nullable = false)
    private Serializable id;

    /** Contact information information */
    private ContactInformation contactInformation = new ContactInformation();

    /** Relationship between this entity and another */
    private SortedSet<Relationship> relationships = new TreeSet<Relationship>();

    /**
     * The unique database id for the object
     * @return Serializable
     */
	public Serializable getId() {
		return id;
	}

    /**
     * Sets the unique database id
     * @param id The new value
     */
    public void setId(Serializable id) {
        this.id = id;
    }

    /**
     * Contact information
     * @return ContactInformation
     */
    public ContactInformation getContactInformation() {
        if (this.contactInformation == null) {
            this.contactInformation = new ContactInformation();
        }
        return this.contactInformation;
    }

    /**
     * Contact information
     * @param contactInformation The new value
     */
    public void setContactInformation(ContactInformation contactInformation) {
        ContactInformation old = getContactInformation();
        this.contactInformation = contactInformation;
        firePropertyChange("contactInformation", old, getContactInformation());
    }

    /**
     * Gets the person's telephone number
     * @return String
     */
    public String getTelephoneNo() {
        return this.getContactInformation().getTelephoneNo();
    }

    /**
     * Gets the person's cellphone number
     * @return String
     */
    public String getCellphoneNo() {
        return this.getContactInformation().getCellphoneNo();
    }

    /**
     * Gets the person's facsimile number
     * @return String
     */
    public String getFacsimile() {
        return this.getContactInformation().getFacsimileNo();
    }

    /**
     * Gets the person's email
     * @return String
     */
    public String getEmail() {
        return this.getContactInformation().getEmail();
    }

    /**
     * Gets the person's postal address
     * @return Address
     */
    public Address getPostalAddress() {
        return this.getContactInformation().getPostalAddress();
    }

    /**
     * Gets the person's physical address
     * @return Address
     */
    public Address getPhysicalAddress() {
        return this.getContactInformation().getPhysicalAddress();
    }

    /**
     * Gets the relationships of the entity
     * @return SortedSet
     */
    public SortedSet<Relationship> getRelationships() {
        if (this.relationships == null) {
            this.relationships = new TreeSet<Relationship>();
        }
        return this.relationships;
    }

    /**
     * Sets the relationships of the entity
     * @param relationships The new value
     */
    public void setRelationships(SortedSet<Relationship> relationships) {
        this.relationships = relationships;
    }

    /**
     * Saves the entity to the persistent store
     * @param ApplicationManager The application manager
     * @throws xperiment.metaphor.model.ModelException If there was an error
     */
    public Entity save(ApplicationManager applicationManager) throws ModelException {

        Dao dao = null;

        try {
            dao = (Dao) applicationManager.getDao();
        } catch (ServiceException e) {
            throw new SaveException("Unable to save entity", e);
        }

        /*
         * Save the relationships and if an Lazy Initialization Exception
         * is thrown then there are no relationships that were changed
         */
        try {
            for (Relationship relationship : relationships) {
                if (relationship != null) {
                    relationship.attach(applicationManager);
                    relationship.save();
                }
            }
        } catch (LazyInitializationException e) {
            /* Ignore */
        } catch (Exception e) {
            throw new SaveException("Unable to save entity relationships", e);
        }
        
        dao.persist(this);
        return this;
    }
}
